package com.fs.util;

import com.fs.entity.Students;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author suke
 * @version 1.0
 * @title UserContext
 * @description 用户上下文:   登录成功的用户信息保存到session域中
 *  从Session域获取User
 * @create 2024/5/17 9:24
 */
public class StudentsContext {
    private static final  String LOGIN_STUDENTS = "current_students";

    //Spring提供了获取javaWeb核心对象的工具类:  RequestContextHolder

    public static HttpSession getSession(){
        return  ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }
    /**
     * 把user保存到Session域
     */
    public static void setStudents(Students students){
        getSession().setAttribute(LOGIN_STUDENTS,students);
    }

    public static Students getStudents(){
        return (Students) getSession().getAttribute(LOGIN_STUDENTS);
    }
}
